package pers.yurwisher.rubick.oauth2.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import pers.yurwisher.rubick.common.serializer.CustomGenericFastJsonRedisSerializer;
import pers.yurwisher.rubick.common.utils.ObjectHelper;
import pers.yurwisher.rubick.oauth2.support.CustomOAuth2AuthenticationSerializer;

/**
 * @author yq
 * @date 2020/09/18 14:11
 * @description redis配置
 * @since V1.0.0
 */
@Slf4j
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        CustomGenericFastJsonRedisSerializer fastJsonRedisSerializer = new CustomGenericFastJsonRedisSerializer();
        fastJsonRedisSerializer.setAutoTypeSupport(true);
        fastJsonRedisSerializer.addAccept("org.springframework.security.oauth2.provider.OAuth2Authentication");
        fastJsonRedisSerializer.addAccept("pers.yurwisher.rubick.oauth2.security.OAuth2User");
        fastJsonRedisSerializer.putDeserializer(OAuth2Authentication.class, new CustomOAuth2AuthenticationSerializer());
        return ObjectHelper.getDefaultRedisTemplate(redisConnectionFactory, fastJsonRedisSerializer);
    }

}
